/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.biz.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.HazardousChemicalsTotal;
import me.zhengjie.modules.biz.service.HazardousChemicalsTotalService;
import me.zhengjie.modules.biz.service.dto.HazardousChemicalsTotalQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Admin
 * @website https://eladmin.vip
 * @date 2023-03-10
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "hazardous-chemicals-total管理")
@RequestMapping("/api/hazardousChemicalsTotal")
public class HazardousChemicalsTotalController {

    private final HazardousChemicalsTotalService hazardousChemicalsTotalService;

    @Log("标准物质盘库导出数据")
    @ApiOperation("标准物质盘库导出数据")
    @GetMapping(value = "/exportHazardousChemicalsTotal")
//    @PreAuthorize("@el.check('hazardousChemicalsTotal:list')")
    public void exportHazardousChemicalsTotal(HttpServletResponse response, HazardousChemicalsTotalQueryCriteria criteria) throws IOException {
        hazardousChemicalsTotalService.downloadHazardousChemicalsTotal(hazardousChemicalsTotalService.exportHazardousChemicalsTotal(criteria), response);
    }

    @Log("器材盘库导出数据")
    @ApiOperation("器材盘库导出数据")
    @GetMapping(value = "/exportEquipmentTotal")
    public void exportEquipmentTotal(HttpServletResponse response, HazardousChemicalsTotalQueryCriteria criteria) throws IOException {
        hazardousChemicalsTotalService.exportEquipmentTotal(hazardousChemicalsTotalService.exportDataTotal(criteria), response);
    }

    @Log("化学试剂盘库导出数据")
    @ApiOperation("化学试剂盘库导出数据")
    @GetMapping(value = "/exportChemicalTotal")
    public void exportChemicalTotal(HttpServletResponse response, HazardousChemicalsTotalQueryCriteria criteria) throws IOException {
        hazardousChemicalsTotalService.exportChemicalTotal(hazardousChemicalsTotalService.exportDataTotal(criteria), response);
    }

    @RequestMapping("/list")
    @Log("查询hazardous-chemicals-total")
    @ApiOperation("查询hazardous-chemicals-total")
    @AnonymousAccess
//    @PreAuthorize("@el.check('hazardousChemicalsTotal:list')")
    public ResponseEntity<Object> queryHazardousChemicalsTotal(HttpServletRequest request) {
//        return new ResponseEntity<>(hazardousChemicalsTotalService.queryAll(criteria,pageable),HttpStatus.OK);
        return new ResponseEntity<>(hazardousChemicalsTotalService.queryAllByRequest(request), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增hazardous-chemicals-total")
    @ApiOperation("新增hazardous-chemicals-total")
//    @PreAuthorize("@el.check('hazardousChemicalsTotal:add')")
    public ResponseEntity<Object> createHazardousChemicalsTotal(@Validated @RequestBody HazardousChemicalsTotal resources) {
        return new ResponseEntity<>(hazardousChemicalsTotalService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改hazardous-chemicals-total")
    @ApiOperation("修改hazardous-chemicals-total")
//    @PreAuthorize("@el.check('hazardousChemicalsTotal:edit')")
    public ResponseEntity<Object> updateHazardousChemicalsTotal(@Validated @RequestBody HazardousChemicalsTotal resources) {
        hazardousChemicalsTotalService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除hazardous-chemicals-total")
    @ApiOperation("删除hazardous-chemicals-total")
//    @PreAuthorize("@el.check('hazardousChemicalsTotal:del')")
    public ResponseEntity<Object> deleteHazardousChemicalsTotal(@RequestBody Integer[] ids) {
        hazardousChemicalsTotalService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/queryList")
    public ResponseEntity<Object> queryList() {
        return new ResponseEntity<>(hazardousChemicalsTotalService.queryList(), HttpStatus.OK);
    }

    @GetMapping("/statistics")
    public ResponseEntity<Object> statistics(HazardousChemicalsTotalQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(hazardousChemicalsTotalService.statistics(criteria, pageable), HttpStatus.OK);
    }


    @RequestMapping("/equipmentTotalList")
    @Log("器材盘库")
    @ApiOperation("器材盘库")
    @AnonymousAccess
    public ResponseEntity<Object> equipmentTotalList(HazardousChemicalsTotalQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(hazardousChemicalsTotalService.equipmentTotalList(criteria, pageable), HttpStatus.OK);
    }

    @RequestMapping("/hazardousChemicalsTotalList")
    @Log("标准物质盘库")
    @ApiOperation("标准物质盘库")
    @AnonymousAccess
    public ResponseEntity<Object> hazardousChemicalsTotalList(HazardousChemicalsTotalQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(hazardousChemicalsTotalService.hazardousChemicalsTotalList(criteria, pageable), HttpStatus.OK);
    }

    @RequestMapping("/chemicalTotalList")
    @Log("化学试剂盘库")
    @ApiOperation("化学试剂盘库")
    @AnonymousAccess
    public ResponseEntity<Object> chemicalTotalList(HazardousChemicalsTotalQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(hazardousChemicalsTotalService.chemicalTotalList(criteria, pageable), HttpStatus.OK);
    }


    @PostMapping("/check")
    @Log("检查库存")
    @ApiOperation("检查库存")
    public ResponseEntity<Object> checkInventory(@RequestBody Map<String, String> map) {
        List<String> codeList = new ArrayList<>(List.of(map.get("code").split(",")));
        String result = hazardousChemicalsTotalService.checkInventory(map.get("name"), codeList);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @PostMapping("/checkInventory")
    @Log("检查库存")
    @ApiOperation("检查库存")
    public ResponseEntity<Object> checkInventorySolo(Float num, String code) {
        String result = hazardousChemicalsTotalService.checkInventorySolo(num, code);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
}
